﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.ComponentModel;

namespace FinalCRM.App_Code
{
    [DataObject]
    public class ArrangeAccountID
    {
        [DataObjectField(true, true)]
        public string AccountID { get; set; }
        public string UserName { get; set; }

        public static List<ArrangeAccountID> getAccountID()
        {
            ArrangeAccountID id = null;
            List<ArrangeAccountID> iList = null;
            SqlConnection conn = null;
            try
            {
                conn = new SqlConnection(ConfigurationManager.ConnectionStrings["CRMService"].ConnectionString);
                conn.Open();
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText = "Select AccountID,UserName from CustomerAccount";

                SqlDataReader dr = comm.ExecuteReader();
                while (dr.Read())
                {
                    iList = new List<ArrangeAccountID>();
                    id = new ArrangeAccountID();
                    id.AccountID = (string)dr["AccountID"];
                    id.UserName = (string)dr["UserName"];
                    iList.Add(id);

                }

            }
            catch
            {
            }
            finally
            {
                conn.Close();
            }
            return iList;
        }

    }
    public class CustomerAccountBusinessObject
    {
        public string AccountID { get; set; }
        public string CompanyName { get; set; }
        public string BillingAddress { get; set; }
        public string ShippingAddress { get; set; }
        public string Email { get; set; }
        public string ContactNo { get; set; }
        public string CustomerID { get; set; }
        public string Description { get; set; }
        public string Gender { get; set; }
        public string DateOfBirth { get; set; }
        public string UserName { get; set; }

        public static DataTable viewCustomerList()
        {

            DataTable dt = null;
            SqlConnection conn = null;
            try
            {
                conn = new SqlConnection(ConfigurationManager.ConnectionStrings["CRMService"].ConnectionString);
                conn.Open();
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText = "Select * from CustomerAccount";
                SqlDataReader dr = comm.ExecuteReader();
                dt = new DataTable();
                dt.Load(dr);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                conn.Close();
            }
            return dt;
        }
        public static void InsertCustomerList(CustomerAccountBusinessObject c)
        {
            SqlConnection conn = null;
            try
            {
                conn = new SqlConnection(ConfigurationManager.ConnectionStrings["CRMService"].ConnectionString);
                SqlCommand comm = new SqlCommand();
                conn.Open();
                comm.Connection = conn;
                comm.CommandText = "Insert into CustomerAccount(AccountID,CompanyName,BillingAddress,ShippingAddress,Email,ContactNo,CustomerID,Description,Gender,DateOfBirth,UserName) Values(@AccountID,@CompanyName,@BillingAddress,@ShippingAddress,@Email,@ContactNo,@CustomerID,@Description,@Gender,@DateOfBirth,@UserName)";
                comm.Parameters.AddWithValue("@AccountID", c.AccountID);
                comm.Parameters.AddWithValue("@CompanyName", c.CompanyName);
                comm.Parameters.AddWithValue("@BillingAddress", c.BillingAddress);
                comm.Parameters.AddWithValue("@ShippingAddress", c.ShippingAddress);
                comm.Parameters.AddWithValue("@Email", c.Email);
                comm.Parameters.AddWithValue("@ContactNo", c.ContactNo);
                comm.Parameters.AddWithValue("@CustomerID", c.CustomerID);
                comm.Parameters.AddWithValue("@Description", c.Description);
                comm.Parameters.AddWithValue("@Gender", c.Gender);
                comm.Parameters.AddWithValue("@DateOfBirth", c.DateOfBirth);
                comm.Parameters.AddWithValue("@UserName", c.UserName);
                int rows = comm.ExecuteNonQuery();
                if (rows < 0)
                {

                }
            }
            catch
            {
            }
            finally
            {
                conn.Close();
            }

        }

    }
}